package com.zhangjun.util;

import com.zhangjun.constant.BigDecimalConstants;
import com.zhangjun.entity.ComputedTax;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author 张钧
 * @Description
 * @create 2022-09-20 20:17
 */
public class TaxComputeUtils {
	/**
	 * 计算费率
	 * 计算参考网站
	 * https://t.aies.cn/geshui/shuilv/#laowu
	 * https://zhuanlan.zhihu.com/p/202974007
	 */
	public static ComputedTax computeTax(BigDecimal income){
	    ComputedTax computedTax = new ComputedTax();
		//收入少于800
		if(BigDecimalUtils.le(income,BigDecimalConstants.BASE_LEVEL)){
			//无扣除费用
			computedTax.setDeductingFee(BigDecimal.ZERO)
					//无速算扣除数
					.setQuickCalculateDeduction(BigDecimal.ZERO)
					//无税
					.setTax(BigDecimal.ZERO)
					//扣税后收入为原收入
					.setIncomeAfterTax(income)
					//税率0
					.setRate("0.00%");
		//收入(800,20000] 税率为20%
		}else if(BigDecimalUtils.between(income,BigDecimalConstants.BASE_LEVEL,false,BigDecimalConstants.SECOND_LEVEL,true)){
			//收入(800,4000] 扣除800元
			if(BigDecimalUtils.between(income,BigDecimalConstants.BASE_LEVEL,false,BigDecimalConstants.FIRST_LEVEL,true)){
				computedTax.setDeductingFee(BigDecimalConstants.BASE_LEVEL);
			//收入(4000,20000] 扣除20%
			}else {
				computedTax.setDeductingFee(income.multiply(BigDecimalConstants.BASE_RATE));
			}
			//无速算扣除数
			computedTax.setQuickCalculateDeduction(BigDecimal.ZERO)
					//税费 = (收入 - 扣除费用) * 20%
					.setTax(income.subtract(computedTax.getDeductingFee()).multiply(BigDecimalConstants.BASE_RATE))
					//税率20%
					.setRate("20.00%");
		//收入(20000,50000] 税率为20% + 20% * 50%
		}else if(BigDecimalUtils.between(income,BigDecimalConstants.SECOND_LEVEL,false,BigDecimalConstants.THIRD_LEVEL,true)){
			//扣除20%
			computedTax.setDeductingFee(income.multiply(BigDecimalConstants.BASE_RATE))
					//速算扣除数 2000
					.setQuickCalculateDeduction(new BigDecimal("2000"))
					//税费 = (收入 - 扣除费用) * (20% + 20% * 50%) - 速算扣除数
					.setTax(income.subtract(computedTax.getDeductingFee()).multiply(BigDecimalConstants.BASE_RATE.add(BigDecimalConstants.BASE_RATE.multiply(BigDecimalConstants.INCREASE_RATE))).subtract(computedTax.getQuickCalculateDeduction()))
					//税率30%
					.setRate("30.00%");
		//收入(50000,+∞) 税率为20% + 20% * 100%
		}else if(BigDecimalUtils.gt(income,BigDecimalConstants.THIRD_LEVEL)){
			//扣除20%
			computedTax.setDeductingFee(income.multiply(BigDecimalConstants.BASE_RATE))
					//速算扣除数 7000
					.setQuickCalculateDeduction(new BigDecimal("7000"))
					//税费 = (收入 - 扣除费用) * (20% + 20% * 100%) - 速算扣除数
					.setTax(income.subtract(computedTax.getDeductingFee()).multiply(BigDecimalConstants.BASE_RATE.add(BigDecimalConstants.BASE_RATE)).subtract(computedTax.getQuickCalculateDeduction()))
					//税率40%
					.setRate("40.00%");
		}
		//税后收入 = 收入 - 税费
		computedTax.setIncomeAfterTax(income.subtract(computedTax.getTax()))
				//扣除费用后纳税所得额 = 收入减去扣除费用
				.setAfterDeductingTaxableIncomeAmount(income.subtract(computedTax.getDeductingFee()))
				.setActualRate(getActualRate(computedTax.getTax(),computedTax.getIncomeAfterTax().add(computedTax.getTax())));
		return computedTax;
	}

	/**
	 * 税费 / 收入
	 * @return 获取税率
	 */
	public static String getActualRate(BigDecimal tax, BigDecimal income){
		if(income.equals(BigDecimal.ZERO)){
			return BigDecimalConstants.FORMAT.format(BigDecimal.ZERO);
		}
		BigDecimal result = tax.divide(income,4, RoundingMode.UP);
		return BigDecimalConstants.FORMAT.format(result);
	}
}
